#include "filedialog.h"



//------------------------------------------------------
ProxyModel::ProxyModel (QObject * parent, bool saveMode)
	: QSortFilterProxyModel (parent)
{
	m_saveMode = saveMode;
	m_fsModel = new QFileSystemModel (this);

	setSourceModel (m_fsModel);
}
//--------------------------------------------------------------
Qt::ItemFlags ProxyModel::flags (const QModelIndex &index) const
{
	if (! m_saveMode)
		return Qt::ItemIsSelectable | Qt::ItemIsEnabled;

	QModelIndex fileIndex = mapToSource (index);

	QString fileName = m_fsModel->filePath (fileIndex);
	QFileInfo fileInfo (fileName);

	if (fileInfo.isDir ())
	{
		bool readable = fileInfo.permissions ().testFlag (QFile::ReadUser);

		return readable ? Qt::ItemIsEnabled : Qt::NoItemFlags;
	}
	else
	{
		bool writable = fileInfo.permissions ().testFlag (QFile::WriteUser);

		return writable ? Qt::ItemIsSelectable | Qt::ItemIsEnabled : Qt::NoItemFlags;
	}

}
//---------------------------------------------------------------------------------
bool ProxyModel::lessThan (const QModelIndex &left, const QModelIndex &right) const
{
	// tier les répertoires avant les fichiers : si de même type triés sur leurs noms
	// sinon rép < fic

	if (m_fsModel->isDir (left) == m_fsModel->isDir (right))
	{
		return m_fsModel->data (left).toString () < m_fsModel->data (right).toString ();
	}
	else
	{
		return m_fsModel->isDir (left);
	}
}

//------------------------------------
//------------FileDialog--------------
//------------------------------------
QString FileDialog::getSaveFileName (QWidget *parent,
									const QString &caption,
									const QString &dir,
									const QString &filter,
									QString */*selectedFilter*/,
									Options options,
									const QString& defaultSuffix)
{
	FileDialog fd (parent);

	fd.setWindowTitle (caption);
	fd.setDirectory (dir);
	fd.setNameFilter (filter);
	fd.setOptions (options);
	fd.setAcceptMode (AcceptSave);
	fd.setFileMode (AnyFile);
	fd.setDefaultSuffix (defaultSuffix);

	int result = fd.exec ();

	if (result == Accepted)
		return fd.selectedFiles ().first ();
	else
		return QString ();

}
//-------------------------------------------------------------------
QString FileDialog::getOpenFileName (QWidget *parent,
									 const QString &caption,
									 const QString &dir,
									 const QString &filter,
									 QString */*selectedFilter*/,
									 QFileDialog::Options options,
									 const QString &defaultSuffix)
{
	FileDialog fd (parent);

	fd.setWindowTitle (caption);
	fd.setDirectory (dir);
	fd.setNameFilter (filter);
	fd.setOptions (options);
	fd.setAcceptMode (AcceptOpen);
	fd.setFileMode (AnyFile);
	fd.setDefaultSuffix (defaultSuffix);

	int result = fd.exec ();

	if (result == Accepted)
		return fd.selectedFiles ().first ();
	else
		return QString ();
}
//--------------------------------------------------------------------
QStringList FileDialog::getOpenFileNames (QWidget *parent,
										 const QString &caption,
										 const QString &dir,
										 const QString &filter,
										 QString */*selectedFilter*/,
										 QFileDialog::Options options,
										 const QString &defaultSuffix)
{
	FileDialog fd (parent);

	fd.setWindowTitle (caption);
	fd.setDirectory (dir);
	fd.setNameFilter (filter);
	fd.setOptions (options);
	fd.setAcceptMode (AcceptOpen);
	fd.setFileMode (AnyFile);
	fd.setDefaultSuffix (defaultSuffix);

	int result = fd.exec ();

	if (result == Accepted)
		return fd.selectedFiles ();
	else
		return QStringList ();
}

